<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use kartik\file\FileInput;
use kartik\select2\Select2;
use yii\web\JsExpression;
use app\common\Functions;

/* @var $this yii\web\View */
/* @var $model \yii2mod\rbac\models\BizRuleModel */
/* @var $form ActiveForm */
?>

<div class="rule-item-form">

    <?php $form = ActiveForm::begin([
        'options'=>['class' => 'form-horizontal','enctype'=>'multipart/form-data']
    ]); ?>

        <?php

        echo  $form->field($model, 'id',['labelOptions'=>['style'=>'display:none']])->hiddenInput(['value' => $model->id]);

        //if ($parent_prod_id)
        //{
            echo  $form->field($model, 'parent_id',['labelOptions'=>['style'=>'display:none']])->hiddenInput(['value' => $parent_prod_id]);
        //

        echo  $form->field($model, 'prod_spec_id',['labelOptions'=>['style'=>'display:none']])->hiddenInput(['value' => $prod_spec_id]);

        // 如果是房源修改，需要进行父级产品的设置
        // 同时满足两个条件 一是创建房源非楼盘 而是不是从处跳转的& !$parent_prod_id
        if ($prod_spec_id==10002 ||  $prod_spec_id==10004 || $prod_spec_id==10001 )
        {
//$model->parentProdName
            $parentStyles=[];

            Yii::trace("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
            if ($model->parentProdName)
            {
             //   Yii::trace("@@@@@@@@@@@@@@@@@@@@@");
            //    Yii::trace($model->parentProdName);
             //   $parentStyles['initValueText'] = $model->parentProdName;
              //  $parentStyles['value'] = $model->parentProdName;
            }


//, parent_prod_spec_id:'. $parent_prod_spec_id . '
            $parentStyles['options'] = ['placeholder' => '请选择 ...'];

            $parentStyles['pluginOptions'] = [
            'placeholder' => '查询 ...',
            'allowClear' => true,
            'minimumInputLength' => 2,
            'language' => [
                'errorLoading' => new JsExpression("function () { return '稍等...'; }"),
            ],
            'ajax' => [
                'url' => 'seachprodinst',
                'dataType' => 'json',
                'data' => new JsExpression('function(params) { return {q:params.term,parent_prod_spec_id: ' . $parent_prod_spec_id . '}; }')
            ],
            'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
            'templateResult' => new JsExpression('function(res) { 
            
            return res.text; }'),



            'templateSelection' => new JsExpression('function (res) { 
                    
                    if (res.id!=res.text)
                    {
                        $("input#prodinst-parent_id").val(res.id);
                    }
                    
                     
                    return res.text; }')];

            echo  $form->field($model, 'parent_id',['labelOptions'=>['style'=>'display:none']])->hiddenInput(['value' => $model->parent_id]);

            echo $form->field($model, 'parentProdName')->label(  '所属楼盘名称')->widget(Select2::classname(), $parentStyles);
//                [
//                'initValueText'   =>  $model->parentProdName ,
//
//                'options' => ['placeholder' => '请选择 ...'],
//
//                'pluginOptions' => [
//                    'placeholder' => '查询 ...',
//                    'allowClear' => true,
//                    'minimumInputLength' => 3,
//                    'language' => [
//                        'errorLoading' => new JsExpression("function () { return '稍等...'; }"),
//                    ],
//                    'ajax' => [
//                        'url' => 'seachprodinst',
//                        'dataType' => 'json',
//                        'data' => new JsExpression('function(params) { return {q:params.term}; }')
//                    ],
//                    'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
//                    'templateResult' => new JsExpression('function(res) { return res.text; }'),
//                    'templateSelection' => new JsExpression('function (res) {
//
//                    $("input#prodinst-parent_id").value(res.id);
//                    return res.text; }'),
//
//                ],
  //          ]);

            //echo  $form->field($model, 'parent_id')->label('所属楼盘')->textInput(['value' => $model->parentProdInsts->prod_name]); // parentProdInsts
        }

        echo  $form->field($model, 'prod_name')->label( $prodSpecName . '名称')->textInput(['maxlength' => 64]);

        echo  $form->field($model, 'own_cust_id',['labelOptions'=>['style'=>'display:none']])->hiddenInput(['value' => $model->own_cust_id]);

        $ownCustStyles = [];
      //  Yii::trace("@@@@@@@@@-----------@@@@@@@@@@@");
        if ($model->ownCustName)
        {
            Yii::trace("@@@@@@@@@ownCustName@@@@@@@@@@@@");
            Yii::trace($model->parentProdName);
          //  $ownCustStyles['initValueText'] = $model->ownCustName;
           // $parentStyles['value'] = $model->parentProdName;
        }

        $ownCustStyles['options'] = ['placeholder' => '请选择 ...'];

        $ownCustStyles['pluginOptions'] = [
            'placeholder' => '查询 ...',
            'allowClear' => true,
            'minimumInputLength' => 1,
            'language' => [
                'errorLoading' => new JsExpression("function () { return '稍等...'; }"),
            ],
            'ajax' => [
                'url' => 'seachcust',
                'dataType' => 'json',
                'data' => new JsExpression('function(params) { return {q:params.term}; }')
            ],
            'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
            'templateResult' => new JsExpression('function(res) { 
            
            return res.text; }'),



            'templateSelection' => new JsExpression('function (res) { 
                    
                    if (res.id!=res.text)
                    {
                        $("input#prodinst-own_cust_id").val(res.id);
                    }
                    
                     
                    return res.text; }')];

        echo $form->field($model, 'ownCustName')->label( '拥有客户名称')->widget(Select2::classname(), $ownCustStyles);


        // province
        // 如果类型是区域的话展示联动列表
        echo $form->field($model, "province")->label('区域')->dropDownList( ArrayHelper::map($regionList, 'id', 'region_name'), [
            'prompt'=>'--请选择省--',

            'onchange'=>'
                        $(".form-group #prodinst-city").show();
                        $.post("'.yii::$app->urlManager->createUrl('prod/prodinst/site').'?parent_region_id="+$(this).val(),function(data){
                        $("select#prodinst-city").html(data);
                        });',
        ]);
        $regionStyle='display:none';
        if ($update)
        {
            $regionStyle ='display:show';
        }


        echo $form->field($model, "city",['labelOptions'=>['style'=>'display:none']])->label('三级区域')->textInput(['style'=>'display:none']) ->dropDownList( $model->getCityList($model->province),
            [
                'prompt'=>'--请选择市--',
                'style'=>$regionStyle,
                'onchange'=>'
                        $(".form-group #prodinst-region_id").show();
                        $.post("'.yii::$app->urlManager->createUrl('prod/prodinst/site').'?parent_region_id="+$(this).val(),function(data){
                            $("select#prodinst-region_id").html(data);
                        });',
            ]);

        echo $form->field($model, "region_id",['labelOptions'=>['style'=>'display:none']])->label('区域类型')->textInput(['style' => 'display:none;']) ->dropDownList( $model->getCityList($model->city),
            [
                'prompt'=>'--请选择区、县--',
                'style'=>$regionStyle,

            ]);

        if ($prod_spec_id==10002)
        {
            // 楼盘状态
            echo $form->field($model, 'state')->label('房源状态')->dropDownList($domainTypes);
        }





        ?>

    <?php

        foreach ($attrSpecs as $attrSpec)
        {
            $prodAttr = $attrSpec->attrSpecs;

            // 文件
           if ($prodAttr->attr_spec_type_id==2)
           {
               $pluginOptions=[];
               $pluginOptions['initialPreviewAsData'] = true;
               $pluginOptions['showRemove'] = true;
               $pluginOptions['showUpload'] = false;

               if ($update)
               {
                   if ($p1)
                   {
                       $pluginOptions['initialPreview'] = $p1[$prodAttr->attr_spec_id];
                   }

               }

               // echo $form->field($uploadModel, "file[]")->label($prodAttr->attr_spec_name)->widget(FileInput::classname(), [
               //attrSpec[{$prodAttr->attr_spec_id}][content]
               //file[{$prodAttr->attr_spec_id}][]
               echo $form->field($uploadModel, "file[]")->label($prodAttr->attr_spec_name)->widget(FileInput::classname(), [
                   'options' => ['multiple' => true, 'accept' => 'image/*'],
                   'pluginOptions' =>$pluginOptions,
               ])->fileInput();
           }elseif ($prodAttr->attr_spec_type_id==3) // 如果type等于3 需要进行字符转义
           {
               echo $form->field($model, "localProdAttrsInsts[{$prodAttr->attr_spec_id}][content]")->label($prodAttr->attr_spec_name)->dropDownList(Functions::getDomains('attr_spec',$prodAttr->attr_spec_id));

           }

           else
           {
               echo $form->field($model, "localProdAttrsInsts[{$prodAttr->attr_spec_id}][content]")->label($prodAttr->attr_spec_name)->textInput(['maxlength' => 64]);
           }

           echo $form->field($model, "localProdAttrsInsts[{$prodAttr->attr_spec_id}][attrSpecTypeID]",['labelOptions'=>['style'=>'display:none']])->hiddenInput(['value' => $prodAttr->attr_spec_type_id]);



        }


        ?>





        <?php


        //echo $form->field($model, 'region_type_id')->label('区域类型')->textInput();
        ?>




    <div class="form-group">
        <?php echo Html::submitButton($model->getIsNewRecord() ? Yii::t('region', 'Create') : Yii::t('region', 'Update'), [
            'class' => $model->getIsNewRecord() ? 'btn btn-success' : 'btn btn-primary', ]); ?>
        <?php echo Html::a(Yii::t('region', 'Cancel'), Yii::$app->request->getReferrer(), ['class' => 'btn btn-default']); ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

